<?php
/**
 * 站点配置
 */

namespace backend\controllers;


use common\models\Config;
use common\utils\XCache;
use common\utils\XFile;
use yii;

class ConfigController extends BaseController
{
    public function actionIndex()
    {
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post();
            foreach ((array)$data as $key => $row) {
                Config::updateAll(['value' => $row], ['variable' => $key]);
            }
            XCache::configDelete();
        }
        $config = Config::find()->where(['scode' => 'base'])->all();
        return $this->render('index', ['config' => $config]);
    }

    public function actionCustom()
    {
        if (Yii::$app->request->post()) {
            $data = Yii::$app->request->post();
            foreach ((array)$data as $key => $row) {
                Config::updateAll(['value' => $row], ['variable' => $key]);
            }
            XCache::configDelete();
        }
        $config = Config::find()->where(['scode' => 'custom'])->all();
        return $this->render('custom', ['config' => $config]);
    }

    public function actionCustomCreate()
    {
        $model = new Config();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $files=XFile::uploadOne("file");
            if($files['error']!=1) {
                if($files['error']==0){
                    $model->value=$files['path'];
                }
                if ($model->save()) {
                    XCache::configDelete();
                    $this->redirect(['custom'], 301);
                } else {
                    $this->tips("保存失败！");
                }
            }else{
                $this->tips("保存失败！".$files['msg']);
            }
        }
        $model->scode = 'custom';
        $model->type = 'input';
        return $this->render('custom-form', ['model' => $model]);
    }

    public function actionCustomUpdate($id)
    {
        $model = Config::findOne($id);
        $oimg=$model->value;
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $files=XFile::uploadOne("file");
            if($files['error']!=1) {
                if ($files['error'] == 0) {
                    $model->value = $files['path'];
                    if($oimg!=$model->value){
                       @unlink(Yii::getAlias('@frontend/web').$oimg);
                    }
                }
                if ($model->save()) {
                    XCache::configDelete();
                    $this->redirect(['custom'], 301);
                } else {
                    $this->tips("保存失败！");
                }
            }else{
                $this->tips("保存失败！".$files['msg']);
            }
        }
        return $this->render('custom-form', ['model' => $model]);
    }

    public function actionCustomDelete($id)
    {
        $model = Config::findOne($id);
        if ($model) {
            if($model->type=='file'){
                @unlink(Yii::getAlias('@frontend/web').$model->value);
            }
            if ($model->delete()) {
                XCache::configDelete();
                $this->redirect(['custom'], 301);
            } else {
                $this->tips("删除失败！");
            }
        } else {
            $this->tips("记录已经不存在！");
        }
    }
}